package com.sphd.beautysalon.model;


import com.android.volley.VolleyError;
import com.sphd.beautysalon.base.BaseModel;
import com.sphd.beautysalon.info.MoneyRecordInfo;
import com.sphd.beautysalon.interfaces.VolleyJsonObjectCallBack;
import com.sphd.beautysalon.presenter.MoneyRecordPresenterImpl;
import com.wzh.frame.utils.JsonUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
* Created by Hu on 2016/12/30
*/

public class MoneyRecordModelImpl implements MoneyRecordModel{

    @Override
    public void sendUrl(String url, MoneyRecordPresenterImpl moneyRecordPresenter) {
        BaseModel.volleyUtil.setJsonObjectRequest(url, new VolleyJsonObjectCallBack() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    List<MoneyRecordInfo> moneyRecordInfoList = JsonUtil.getListFromString(response.getString("list"),MoneyRecordInfo.class);
                    double sum_account =response.getDouble("sum_account");
                    moneyRecordPresenter.sumAccount(sum_account);
                    moneyRecordPresenter.getList(moneyRecordInfoList);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
            moneyRecordPresenter.sendError(error);
            }
        });
    }

    @Override
    public void setResUrl(String url, MoneyRecordPresenterImpl moneyRecordPresenter) {
        BaseModel.volleyUtil.setJsonObjectRequest(url, new VolleyJsonObjectCallBack() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    int res =response.getInt("res");
                    moneyRecordPresenter.recRes(res);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                moneyRecordPresenter.sendError(error);
            }
        });
    }

    @Override
    public void setOpenCloseResUrl(String url, MoneyRecordPresenterImpl moneyRecordPresenter) {
        BaseModel.volleyUtil.setJsonObjectRequest(url, new VolleyJsonObjectCallBack() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    int res =response.getInt("res");
                    moneyRecordPresenter.recOpenCloseRes(res);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                moneyRecordPresenter.sendError(error);
            }
        });
    }
}